/**
 * 部门结构控制器
 */
import $_fn from '~plugins'
import './index.less'
export default ['$scope','$rootScope','$location','$ui','http','$compile',function($scope,$rootScope,$location,$ui,http,$compile) {
    $scope.$_fn = $_fn
	//四级按钮
	$scope.tabMenu = $_fn.getCurrMenu($location.path())
    console.log($scope.tabMenu)
    $scope.subMenu = $scope.tabMenu.children[1].children
    console.log($scope.subMenu)
	console.log('催收管理控制器')
	//四级按钮页面跳转方法
	$scope.operation = function (path,item) {
		console.log('111')
        var _path = path.split('@')
        let params = JSON.stringify(item)
        if(_path[0] == 'link'){
            console.log(path)
            $state.go(_path[1],{id:1,ids:2})
        }
        if(_path[0] == 'alert'){
            $ui.modal({content:"<"+_path[1]+" params="+params+"></"+_path[1]+">"})
        }
   }
	var j=''
	var htmlbtn=function(content){
			$.each(content, function(i,t) {
				console.log(t)
				j+='j';
				$scope[j]=t;
				if(t.children && t.children.length != 0){
					html.push('<tr class="tr1" data-type ="true" data-has-child="true" data-focus="false">');
					html.push('<td>[-]</td>');
				}else{
					html.push('<tr class="tr1" data-type ="true" data-has-child="false" data-focus="false">');
					html.push('<td></td>');
				}
				html.push('<td>'+t.name+'</td>');
				html.push('<td>');
				/*html.push('<span  class="_yellow" data-attr="addSection" data-type="2" data-conp="'+ t.name +'" data-id="'+ t.id +'">');
				html.push('添加子部门');
				html.push('</span>');
				html.push('<span  class="second-span _blue" data-attr="comSection" data-id="'+ t.id +'" data-parentId="'+ t.parentId +'">');
				html.push('编辑部门');
				html.push('</span>');
				html.push('<span  class="second-span _red" data-attr="remSection" data-id="'+ t.id +'">删除部门</span>');*/
				html.push('<dropdown placeholder="请选择" ng-click="$event.stopPropagation()">');
                html.push('<li ng-repeat="(i,list) in subMenu track by $index" ng-if="list.state==1"'+ 
                		  'ng-click="operation(list.path,{id:'+j+'.id,name:'+j+'.name,parentId:'+j+'.parentId})">'+
                    	  '{{list.name}}'+
                          '</li>');
                html.push('</dropdown>')
				html.push('</td>');
				html.push('</tr>');
				htmlbtn1(t.children,'tr11');
			});
		}
	var k='';
	var htmlbtn1=function(content,ii){
			$.each(content, function(i,t) {
				console.log(t)
				k+='l'
				$scope[k]=t;
				if(t.children && t.children.length != 0){
					console.log("class",ii)
					html.push('<tr class="'+ii+'" data-type ="true" data-has-child="true" data-focus="false">');
					html.push('<td>[-]</td>');
				}else{
					html.push('<tr class="'+ii+'" data-type ="true" data-has-child="false" data-focus="false">');
					html.push('<td></td>');
				}
				html.push('<td>');
				html.push('<span class="toggle-tu">');
				html.push('<span class="tu-1"></span>');
				html.push('<span class="tu-2"></span>');
				html.push('</span>');
				html.push(t.name);
				html.push('</td>');
				html.push('<td>');
				
				
				/*<dropdown placeholder="请选择" ng-click="$event.stopPropagation()">
                    <li ng-repeat="(i,list) in subMenu track by $index" ng-if="list.state==1" 
                    	ng-click="operation(list.path,{id:item.id,name:item.name})">
                    	{{list.name}}
                    </li>
                </dropdown>*/
                html.push('<dropdown placeholder="请选择" ng-click="$event.stopPropagation()">');
                /*html.push('<li ng-repeat="(i,list) in subMenu track by $index" ng-if="list.state==1&&list.name!="'+'"删除部门"'+
                		  '" ng-click="operation(list.path,{id:item.id,name:item.name})">'+
                    	  '{{list.name}}'+
                          '</li>'+
                          '<li ng-repeat="(i,list) in subMenu track by $index" ng-if="list.state==1&&list.name=="'+'"删除部门"'+'&&'+t.children+ 
                		  '" ng-click="operation(list.path,{id:item.id,name:item.name})">'+
                    	  '{{list.name}}'+
                          '</li>'
                          );*/
                html.push('<li ng-repeat="(i,list) in subMenu track by $index" ng-if="list.state==1"'+ 
                		  'ng-click="operation(list.path,{id:'+k+'.id,name:'+k+'.name,parentId:'+k+'.parentId})">'+
                    	  '{{list.name}}'+
                          '</li>');
                html.push('</dropdown>')
				/*if(ii!='tr1111'){
					html.push('<span  class="_yellow" data-attr="addSection" data-type="2" data-conp="'+ t.name +'" data-id="'+ t.id +'">');
					html.push('添加子部门');
					html.push('</span>');
				}
				html.push('<span  class="second-span _blue" data-attr="comSection" data-id="'+ t.id +'" data-parentId="'+ t.parentId +'">');
				html.push('编辑部门');
				html.push('</span>');
				html.push('<span  class="second-span _red" data-attr="remSection" data-id="'+ t.id +'">删除部门</span>');*/
				html.push('</td>');
				html.push('</tr>');
				
				if(t.children && t.children.length !=0){
					console.log("--------",ii+1)
					htmlbtn1(t.children,ii+1);
				}
				
			});
		};
	//部门结构初始化
	var html=[]
	$scope.xinXi=function(){
		http.post('deptTreeInit.do',$scope.param,function (data) {
            if(data.errcode > 0){
                /*if(data.data.orderList.list.length === 0){
                    $scope.isData = true
                    $scope.$apply()
                }*/
                $scope.$apply(function () {
                    htmlbtn(data.data);
					//$scope.html1=$sce.trustAsHtml(html.join(''));
					//将动态的指令渲染
					var compileFn = $compile(html.join(''));
					var $dom = compileFn($scope);
					console.log($dom)
					$("#htmlBody").html($dom)
					
                })
                $scope.trClick()
            }else{
            	$ui.toast({
					text:data.errmsg,
					type:'warning'
				})
            }
        },function(){
            //报错时
            $scope.isData = true
            $scope.$apply()
        })
	}
	$scope.xinXi()
	//部门展开收起
	$scope.trClick = function(){
			$('.table-toggle-ceng tbody tr').on('click',function(){	
				console.log($(this).attr('data-focus'));
				if($(this).attr('data-focus') == 'false'){
					$(this).css('background-color','rgb(255, 251, 244)');
					$(this).attr('data-focus','true');
				}else if($(this).attr('data-focus') == 'true'){
					$(this).css('background-color','');
					$(this).attr('data-focus','false');
				}
				var trs=$('.table-toggle-ceng tbody tr');
				var th=$(this).attr('class').length;
				//console.log(th);
				var fal=$(this).attr('data-type');
				var trNow = this;
				var trIndex;
				trs.each(function(index,ele){
					if(trNow == ele){
						//console.log(index);
						trIndex = index;
					}
				});
				if(fal=='false'){
					for(var i=trIndex+1;i<trs.length;i++){
						if(th<trs[i].className.length){
							$(trs[i]).show();
							$(trs[i]).find('td').eq(0).text('[-]');
							$(trs[i]).attr('data-type','true');
							if($(trs[i]).attr('data-has-child') == 'false'){
								//console.log('data-has-child');
								$(trs[i]).find('td').eq(0).html('');
							}
						}else{
							i = trs.length;
						}
					}
					if($(this).attr('data-has-child') == 'true'){
						$(this).find('td').eq(0).text('[-]');
						$(this).attr('data-type','true');
					}
				}else if(fal=='true'){
					for(var j= trIndex+1;j<trs.length;j++){
						if(th<trs[j].className.length){
							$(trs[j]).hide();
							$(trs[j]).find('td').eq(0).text('[+]');
							$(trs[j]).attr('data-type','false');
							if($(trs[j]).attr('data-has-child') == 'false'){
								$(trs[j]).find('td').eq(0).html('');
							}
						}else{
							j = trs.length;
						}
					}
					if($(this).attr('data-has-child') == 'true'){
						$(this).find('td').eq(0).text('[+]');
						$(this).attr('data-type','false');
					}
				}
			});
		};
	//获取数据的函数
    $scope.getDada = function(){
        //$scope.param.pageNum = page
        http.post('selectAfterLoanCollectionInit.do',$scope.param,function (data) {
            /*var _data = data.data
            $scope.page.pageNum =  $scope.page.current
            $scope.page.pageSize = _data.pageSize
            $scope.page.total = _data.total*/
            if(data.errcode > 0){
                //列表无数据
                if(data.data.orderList.list.length === 0){
                    $scope.isData = true
                    $scope.$apply()
                }
                $scope.$apply(function () {
                    $scope.param1.dataList = data.data.orderList.list;
                    $scope.param1.orderList = data.data
                   // $scope.param1.stateList = data.data.stateList;//状态
                   // $scope.param1.applyTermList = data.data.applyTermList;//期限
                })
            }else{
            	$ui.toast({
					text:data.errmsg,
					type:'warning'
				})
            }
        },function(){
            //报错时
            $scope.isData = true
            $scope.$apply()
        })
    }
}]
